﻿using System.Collections.Generic;
using DAL;
using Entity;
using Utilities;

namespace Business
{
	public class OutProductBusiness
	{
		#region code CMS
        
        public static List<OutProduct> GetOutProductListByPaging(bool isParent, string name, string code, int cate, int stt, int pageIndex, int pageSize, ref int totalRecord)
        {
            var result = FreshCmsDataAccessProvider.OutProduct.GetOutProductListByPaging(isParent, name, code, cate, stt, pageIndex, pageSize, ref totalRecord);
            return result;
        }

        public static OutProduct GetOutProductById(int proId)
        {
            var result = FreshCmsDataAccessProvider.OutProduct.GetOutProductById(proId);
            if(result!=null)
            {
                result.ThumbImage1 = ImageUtils.GetImageThumb(result.ProImage1, Constant.ProductAvatar1Width,
                                                              Constant.ProductAvatar1Height,
                                                              Constant.ProductAvatarDefault);
                if (result.ProHot == 1) result.ChkHot = "checked";
                if (result.ProNew == 1) result.ChkNew = "checked";
                if (result.ProIsStock == 1) result.ChkStock = "checked";
            }
            return result;
        }

        public static int InsertOutProduct(OutProduct OutProduct)
        {
            var result = FreshCmsDataAccessProvider.OutProduct.InsertOutProduct(OutProduct);
            return result;
        }

        /// <summary>
        /// Hàm UPDATE cho User, có check đúng UserID mới cho UPDATE. Return: -3: không phải bài của ng Update, >0 là ok
        /// </summary>
        /// <param name="OutProduct"></param>
        /// <returns></returns>
        public static int UpdateOutProductWithRole(OutProduct OutProduct)
        {
            var result = FreshCmsDataAccessProvider.OutProduct.UpdateOutProductWidthRole(OutProduct);
            return result;
        }

        /// <summary>
        /// Hàm UPDATE cho Admin, UPDATE ngay không check USERID
        /// </summary>
        /// <param name="OutProduct"></param>
        /// <returns></returns>
        public static int UpdateOutProduct(OutProduct OutProduct)
        {
            var result = FreshCmsDataAccessProvider.OutProduct.UpdateOutProduct(OutProduct);
            return result;
        }

        /// <summary>
        ///  Delete sản phẩm cho Super ADMIN (dùng cho xóa vĩnh viễn)
        /// </summary>
        /// <param name="proId"></param>
        /// <returns></returns>
        public static int DeleteOutProduct(int proId)
        {
            var result = FreshCmsDataAccessProvider.OutProduct.DeleteOutProduct(proId);
            return result;
        }

        /// <summary>
        /// Delete sản phẩm có check đúng tài khoản mới Delete (dùng cho xóa vĩnh viễn)
        /// </summary>
        /// <param name="proId"></param>
        /// <param name="userid"></param>
        /// <returns></returns>
        public static int DeleteOutProductWithRole(int proId, int userid)
        {
            var result = FreshCmsDataAccessProvider.OutProduct.DeleteOutProductWidthRole(proId, userid);
            return result;
        }

        /// <summary>
        /// Update status sản phẩm cho Super ADMIN (dùng cho xóa tạm thời và phục hồi)
        /// </summary>
        /// <param name="proId"></param>
        /// <returns></returns>
        public static int UpdateOutProductStatus(int proId, int proStt)
        {
            var result = FreshCmsDataAccessProvider.OutProduct.UpdateOutProductStatus(proId, proStt);
            return result;
        }

        /// <summary>
        /// Update status sản phẩm dành cho người dùng có check Role (dùng cho xóa tạm thời và phục hồi)
        /// </summary>
        /// <param name="proId"></param>
        /// <param name="proStt"></param>
        /// <param name="userid"></param>
        /// <returns></returns>
        public static int UpdateOutProductStatusWithRole(int proId, int proStt, int userid)
        {
            var result = FreshCmsDataAccessProvider.OutProduct.UpdateOutProductStatusWidthRole(proId, proStt, userid);
            return result;
        }
        
        /// <summary>
        /// Hàm UPDATE FAST cho User, có check đúng UserID mới cho UPDATE. Return: -3: không phải bài của ng Update, >0 là ok
        /// </summary>
        /// <param name="OutProduct"></param>
        /// <returns></returns>
        public static int UpdateOutProductWithRole_Fast(OutProduct OutProduct)
        {
            var result = FreshCmsDataAccessProvider.OutProduct.UpdateOutProductWidthRole_Fast(OutProduct);
            return result;
        }

        /// <summary>
        /// Hàm UPDATE FAST cho Admin, UPDATE ngay không check USERID
        /// </summary>
        /// <param name="OutProduct"></param>
        /// <returns></returns>
        public static int UpdateOutProduct_Fast(OutProduct OutProduct)
        {
            var result = FreshCmsDataAccessProvider.OutProduct.UpdateOutProduct_Fast(OutProduct);
            return result;
        }

        /// <summary>
        /// Check xem bai viet co dung theo ng viet hay khong, tra ve > 0 la ok
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="proid"></param>
        /// <returns></returns>
        public static int CheckRole(int userid, int proid)
        {
            var result = FreshCmsDataAccessProvider.OutProduct.CheckRole(userid, proid);
            return result;
        }

        public static List<OutProduct> GetOutProductForBox(int top, int cate, int widthImgBox, int heightImgBox)
        {
            var result = FreshCmsDataAccessProvider.OutProduct.GetOutProductListForBox(top, cate);
            Format(result, widthImgBox, heightImgBox);
            return result;
        }

		#endregion

        #region Format data

        private static void Format(List<OutProduct> lst, int width, int height = 0)
        {
            foreach (var item in lst)
            {
                item.ThumbImage1 = getThumbProduct(item);
                item.ThumbBoxImage = getThumbProductForBox(item, width, height);
                item.ProLinkDetail = getLinhDetailProduct(item);
                item.ProNameForJs = Utils.formatTextJs(item.ProName);
            }
        }

        private static string getThumbProduct(OutProduct obj)
        {
            return obj.ProImage1 + ".thumb" + Constant.ProductAvatar1Width + "x" + Constant.ProductAvatar1Height + ".jpg";
        }
        private static string getThumbProductForBox(OutProduct obj, int widthImgBox, int heightImgBox)
        {
            return obj.BoxImage + ".thumb" + widthImgBox + "x" + heightImgBox + ".jpg";
        }
        private static string getLinhDetailProduct(OutProduct obj)
        {
            return "/chi-tiet-san-pham/" + UnicodeUtility.UnicodeToKoDauAndGach(obj.ProName) + "-" + obj.ProId + ".htm";
        }
        #endregion
	}
}